package com.joker.microstoremanager.controller;

import com.joker.common.model.JokerResponse;
import com.joker.microstoremanager.model.Goods;
import com.joker.microstoremanager.model.MicroShop;
import com.joker.microstoremanager.service.GoodsService;
import com.joker.microstoremanager.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.joker.microstoremanager.context.UserContext.getUser;

@RestController
@RequestMapping("api")
@Slf4j
public class ApiController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private TagService tagService;

    @RequestMapping("skuChange")
    public JokerResponse<String> skuChange(String skuNo, Integer action, Long goodsId){
        MicroShop user = getUser();
        String message = goodsService.skuChange(skuNo,action,user,goodsId);
        log.info("skuChange {} message is {}",message,skuNo);
        JokerResponse<String> response = new JokerResponse<>();
        response.setMessage(message);
        return response;
    }

    @RequestMapping("list")
    public JokerResponse<List<Goods>> listGoods(@RequestParam(value = "page", defaultValue = "0") Integer page){
        log.info("Android Request received!");
        Pageable request = new PageRequest(page,50);
        MicroShop user = (MicroShop) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        List<Goods> goods = goodsService.findAllGoods(user,request).getContent();
        JokerResponse<List<Goods>> response = new JokerResponse<>();
        response.setData(goods);
        return response;
    }

    @RequestMapping("loginCheck")
    public JokerResponse<String> login(){
        return new JokerResponse<>("success");
    }

    @GetMapping("delTag")
    public JokerResponse<String> delTag(Long id){
        tagService.deleteById(id);
        return new JokerResponse<>("success");
    }
}
